package com.yeepay.demo;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.epos.util.CallbackUtils;
import com.epos.util.DigestUtil;

/**
 * 
 * <p> EPOS测试用消费</p>
 * @Title: EposPayUtill.java
 * @Projectname: yeepaytest
 * @Package: com.yeepay.epos 
 * @Copyright: Copyright (c)2014
 * @Company: YeePay
 * @version: 1.0
 * @Create: 2014年3月9日
 */
public class EposDemo001 {
	private static final Log log = LogFactory.getLog(EposDemo001.class);
	
	public static String EPOSSALE_TYPE="EposSale";
	public static String EPOSREQUESTURL="https://www.yeepay.com/app-merchant-proxy/command";
	public static String CUSTOMER_NUMBER="10011206975";
	public static String CUSTOMER_KEY="M6N9DW05r1iCZ07wNdfo40s6GD8469626zPouz136NKm4g1271fs63604271";
	
	/**
	 * 
	 * <p>EPOS消费接口</p>
	 * @Title eposSale
	 * @param p2_Order
	 * @param p3_Amt
	 * @param p4_Cur
	 * @param p5_Pid
	 * @param p8_Url
	 * @param pa_CredType
	 * @param pb_CredCode
	 * @param pd_FrpId
	 * @param pe_BuyerTel
	 * @param pf_BuyerName
	 * @param pt_ActId
	 * @param pa2_ExpireYear
	 * @param pa3_ExpireMonth
	 * @param pa4_CVV
	 * @param prisk_TerminalCode
	 * @param prisk_Param
	 * @param pr_NeedResponse
	 * @return
	 * @throws Exception
	 * @return Map<String,String> 
	 * @Create 2013年12月24日 String prisk_Param
	 */
	public static Map<String,String> eposSale
	(String p2_Order,String p3_Amt,String p4_Cur,String p5_Pid,String p8_Url,String pa_CredType,String pb_CredCode,String pd_FrpId,
			String pe_BuyerTel,String pf_BuyerName,String pt_ActId,String pa2_ExpireYear,String pa3_ExpireMonth,String pa4_CVV,
			String prisk_TerminalCode,String pr_NeedResponse)throws Exception{
		return eposSale( CUSTOMER_NUMBER,CUSTOMER_KEY,p2_Order, p3_Amt, p4_Cur, p5_Pid, p8_Url, pa_CredType, pb_CredCode, pd_FrpId,
				 pe_BuyerTel, pf_BuyerName, pt_ActId, pa2_ExpireYear, pa3_ExpireMonth, pa4_CVV,
				 prisk_TerminalCode, pr_NeedResponse);
	}
	/**
	 * 
	 * <p>EPOS消费接口</p>
	 * @Title eposSale
	 * @param p1_MerId
	 * @param hmacKey
	 * @param p2_Order
	 * @param p3_Amt
	 * @param p4_Cur
	 * @param p5_Pid
	 * @param p8_Url
	 * @param pa_CredType
	 * @param pb_CredCode
	 * @param pd_FrpId
	 * @param pe_BuyerTel
	 * @param pf_BuyerName
	 * @param pt_ActId
	 * @param pa2_ExpireYear
	 * @param pa3_ExpireMonth
	 * @param pa4_CVV
	 * @param prisk_TerminalCode
	 * @param prisk_Param
	 * @param pr_NeedResponse
	 * @return
	 * @throws Exception
	 * @return Map<String,String> 
	 * @Create 2013年12月24日
	 */
	public static Map<String,String> eposSale
	(String p1_MerId,String  hmacKey,String p2_Order,String p3_Amt,String p4_Cur,String p5_Pid,String p8_Url,String pa_CredType,String pb_CredCode,String pd_FrpId,
			String pe_BuyerTel,String pf_BuyerName,String pt_ActId,String pa2_ExpireYear,String pa3_ExpireMonth,String pa4_CVV,
			String prisk_TerminalCode,String pr_NeedResponse)throws Exception{
		String p0_Cmd = EPOSSALE_TYPE;
		Map<String,String>  resultMap=null; 
		//拼装需要签名的字符串
		StringBuffer sbfTemp=new StringBuffer();
		sbfTemp.append(p0_Cmd);//业务类型
		sbfTemp.append(p1_MerId);//商户编号
		sbfTemp.append(p2_Order);//商户订单号
		sbfTemp.append(p3_Amt);//消费金额
		sbfTemp.append(p4_Cur);//交易币种
		sbfTemp.append(p5_Pid);//商品名称
		sbfTemp.append(p8_Url);//接收支付结果地址
		sbfTemp.append(pa_CredType);//证件类型
		sbfTemp.append(pb_CredCode);//证件号码
		sbfTemp.append(pd_FrpId);//银行编码
		sbfTemp.append(pe_BuyerTel);//消费者手机号
		sbfTemp.append(pf_BuyerName);//消费者姓名
		sbfTemp.append(pt_ActId);//信用卡卡号
		sbfTemp.append(pa2_ExpireYear);//有效期（年）
		sbfTemp.append(pa3_ExpireMonth);//有效期（月）
		sbfTemp.append(pa4_CVV);//CVV
		sbfTemp.append(prisk_TerminalCode);//终端号
//		sbfTemp.append(prisk_Param);//风控扩展参数 
		String hmac=DigestUtil.hmacSign(sbfTemp.toString(), hmacKey);
		log.info("请求参数："+sbfTemp.toString());
		
		Map<String,String> hm=new HashMap<String,String>();	
		hm.put("p0_Cmd",p0_Cmd);////业务类型
		hm.put("p1_MerId",p1_MerId);//商户编号
		hm.put("p2_Order",p2_Order);//商户订单号
		hm.put("p3_Amt",p3_Amt);//消费金额
		hm.put("p4_Cur",p4_Cur);//交易币种
		hm.put("p5_Pid",p5_Pid);//商品名称
		hm.put("p8_Url",p8_Url);//接收支付结果地址
		hm.put("pa_CredType",pa_CredType);//证件类型
		hm.put("pb_CredCode",pb_CredCode);//证件号码
		hm.put("pd_FrpId",pd_FrpId);//银行编码
		hm.put("pe_BuyerTel",pe_BuyerTel);//消费者手机号
		hm.put("pf_BuyerName",pf_BuyerName);//消费者姓名	
		hm.put("pt_ActId",pt_ActId);//信用卡卡号
		hm.put("pa2_ExpireYear",pa2_ExpireYear);//有效期（年）
		hm.put("pa3_ExpireMonth",pa3_ExpireMonth);//有效期（月）
		hm.put("pa4_CVV",pa4_CVV);//CVV
		hm.put("prisk_TerminalCode", prisk_TerminalCode);//终端号
//		hm.put("prisk_Param", prisk_Param);//风控扩展参数
		hm.put("pr_NeedResponse",pr_NeedResponse);//需要应答
		hm.put("hmac", hmac);
		
		String result=CallbackUtils.httpRequest(EPOSREQUESTURL, hm, "POST", "gbk");
		
	 	log.info("yeep返回值："+result);
		if(isEmpty(result)) throw new Exception("result is null");
		
		//将返回result分割成map
		resultMap=CallbackUtils.queryString(result,"\n");
		log.info(" resultMap is null:"+resultMap==null);
		if(resultMap==null) throw new Exception("resultMap is null");
		
		return resultMap;
	}
	
	public static boolean isEmpty(String str){
		if( null== str || "".equals(str.replace(" ", ""))){
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) throws Exception {
		String p2_Order = "804011824001";//"test001";
		String p3_Amt="0.10";
		String p4_Cur="CNY";
		String p5_Pid="flight";
		String p8_Url="http://cjqhut.6655.la:25844/YeepayDemo/callback.jsp";
		String pa_CredType="IDCARD";
		String pb_CredCode="362334198911112813";//"370523198804260038";
		String pd_FrpId = "BOCOCREDIT";
		String pe_BuyerTel="13716372979";//"15215317596";
		String pf_BuyerName="程建强";
		String pt_ActId="4581230922953684";//"4062522900966872";
		String pa2_ExpireYear="2019";//"2020";;
		String pa3_ExpireMonth="12";//"12";
		String pa4_CVV="8051";//"973";
		String prisk_TerminalCode="";
//		String prisk_Param = "{\"industry\":\"4511\",\"air_IsMember\":\"1\",\"air_ApplicationMode\":\"B2C\",\"air_Phone\":\"13120227700\",\"air_ContactPhone\":\"13120227700\",\"air_ContactName\":\"杜志强\",\"air_Flight\":[{\"air_FlightNo\":\"MU5102\",\"air_Cabin\":\"N\",\"air_STime\":\"20140402080000\",\"air_ETime\":\"20140402101000\",\"air_SCity\":\"PEK\",\"air_ECity\":\"SHA\"}],\"air_Passenger\":[{\"air_Name\":\"李强\",\"air_CertificateType\":\"OTHER\",\"air_CertificateNo\":\"11111111\",\"air_Email\":\"\",\"air_FlightNo\":\"MU5102\",\"air_STime\":\"20140402080000\",\"air_Phone\":\"\",\"air_Pnr\":\"\",\"air_TicketNo\":\"\",\"air_Type\":\"00\"}]}";
		String pr_NeedResponse="1";
//		eposSale(p2_Order, p3_Amt, p4_Cur, p5_Pid, p8_Url, pa_CredType, pb_CredCode, pd_FrpId, pe_BuyerTel, 
//				 pf_BuyerName, pt_ActId, pa2_ExpireYear, pa3_ExpireMonth, pa4_CVV, prisk_TerminalCode, prisk_Param, pr_NeedResponse);
		eposSale(p2_Order, p3_Amt, p4_Cur, p5_Pid, p8_Url, pa_CredType, pb_CredCode, pd_FrpId, pe_BuyerTel, 
				 pf_BuyerName, pt_ActId, pa2_ExpireYear, pa3_ExpireMonth, pa4_CVV, prisk_TerminalCode, pr_NeedResponse);
		
	}
}
